package org.jeecg.glr.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.util.SysUtil;
import org.jeecg.glr.entity.GlrRadio;
import org.jeecg.glr.entity.GlrRadioServer;
import org.jeecg.glr.service.IGlrRadioServerService;
import org.jeecg.glr.service.IGlrRadioService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.HttpRequestUtil;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: 音柱管理
 * @Author: jeecg-boot
 * @Date:   2020-02-11
 * @Version: V1.0
 */

@Slf4j
@Api(tags = "音柱模块")
@RestController
@RequestMapping("/glr/glrRadio")
public class GlrRadioController extends JeecgController<GlrRadio, IGlrRadioService> {
	@Autowired
	private IGlrRadioService glrRadioService;

	@Autowired
	private IGlrRadioServerService glrRadioServerService;
	
	/**
	 * 分页列表查询
	 *
	 * @param glrRadio
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "音柱模块-分页列表查询")
	@ApiOperation(value = "音柱模块-分页列表查询", notes = "音柱模块-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GlrRadio glrRadio,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) throws Exception {
		Map<String, String[]> parameterMap=new HashMap(req.getParameterMap());
		parameterMap.remove(QueryGenerator.ORDER_COLUMN);
		parameterMap.remove(QueryGenerator.ORDER_TYPE);
	    QueryWrapper<GlrRadio> queryWrapper = QueryGenerator.initQueryWrapperCode(glrRadio, parameterMap);
		queryWrapper.orderByAsc("name");
		queryWrapper.orderByAsc("work_status");
		Page<GlrRadio> page = new Page<GlrRadio>(pageNo, pageSize);
		IPage<GlrRadio> pageList = glrRadioService.page(page, queryWrapper);
		List<GlrRadio> list=pageList.getRecords();

		GlrRadioServer glrRadioServer=glrRadioServerService.doGetByPropertyName("code", GLRConstant.SERVER_CODE_RATTOP);
		String stJSessionID = glrRadioServerService.getRattopSessionId(glrRadioServer);

		for (GlrRadio domain: list) {
			glrRadioServerService.updateGlrRadioStatus(domain,glrRadioServer,stJSessionID);
		}
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param glrRadio
	 * @return
	 */
	@AutoLog(value = "添加音柱",logType= CommonConstant.LOG_TYPE_2)
	@ApiOperation(value = "音柱表-添加", notes = "音柱表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GlrRadio glrRadio) {
	    glrRadio.setStatus(GLRConstant.YN_Y);
		glrRadio=glrRadioServerService.updateGlrRadioServerCodeByCompanyCode(glrRadio);
		glrRadioService.saveEntity(glrRadio);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param glrRadio
	 * @return
	 */
	@AutoLog(value = "修改音柱",logType= CommonConstant.LOG_TYPE_2)
	@ApiOperation(value = "音柱表-修改", notes = "音柱表-修改")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GlrRadio glrRadio) {
		glrRadio=glrRadioServerService.updateGlrRadioServerCodeByCompanyCode(glrRadio);
		glrRadioService.updateEntityById(glrRadio);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "删除音柱",logType= CommonConstant.LOG_TYPE_2)
	@ApiOperation(value = "音柱表-删除", notes = "音柱表-删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		glrRadioService.removeEntityById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "批量删除音柱",logType= CommonConstant.LOG_TYPE_2)
	@ApiOperation(value = "音柱表-批量删除", notes = "音柱表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.glrRadioService.removeEntityByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	@ApiOperation(value = "音柱表-根据id查询", notes = "音柱表-根据id查询")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GlrRadio glrRadio = glrRadioService.getById(id);
		if(glrRadio==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(glrRadio);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param glrRadio
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrRadio glrRadio) {
        return super.exportXls(request, glrRadio, GlrRadio.class, "音柱管理");
    }

    /**
    * 通过excel导入数据
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrRadio.class);
    }


	/**
	 *  设置音量
	 * @param glrRadio
	 * @return
	 */
	@AutoLog(value = "设置音量",logType= CommonConstant.LOG_TYPE_2)
	@ApiOperation(value = "音柱表-设置音量", notes = "音柱表-设置音量")
	@PostMapping(value = "/setRadioVol")
	public Result<?> setRadioVol(@RequestBody GlrRadio glrRadio) {
		glrRadioServerService.setRadioVol(glrRadio);
		return Result.ok("设置音量成功!");
	}

}
